package org.fluidripple.socket.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.Channel;
import org.fluidripple.socket.inbound.ClientEventHandler;
import org.fluidripple.socket.operation.ChannelOperation;
import org.fluidripple.socket.session.Magnification;
import org.fluidripple.socket.session.Session;
import org.fluidripple.socket.session.SessionManager;
import org.fluidripple.socket.stickhalf.FixedLengthDecoderImpl;
import org.fluidripple.socket.wrapper.DataWrapper;
import org.springframework.stereotype.Component;

/**
 * 放大倍数
 */
@Component
public class DeviceMagnificationProtocol extends Protocol {
    @Override
    public void writer(Channel channel) {
        ChannelOperation.replaceChannelHandler(channel, ClientEventHandler.name, FixedLengthDecoderImpl.name,new FixedLengthDecoderImpl(7));
        Session session = SessionManager.getSession(channel);
        session.setProtocol(this);
        byte[] bytes = ByteBufUtil.decodeHexDump("F0C103B1B4");
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(5).writeBytes(bytes);
        channel.writeAndFlush(byteBuf);

    }

    @Override
    public void toDecode(DataWrapper dataWrapper) {
        Session session = dataWrapper.getSession();
        ByteBuf buf = dataWrapper.getBuf();
        buf.skipBytes(4);
        int a = buf.readByte();
        int b = buf.readByte();
        //放大倍数
        Magnification magnification = new Magnification(a, b);
        session.setMagnification(magnification);
    }
}
